package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesAdvanceRefund;
import com.kgc.collection.pojo.SalesAdvanceRefundDetail;
import com.kgc.skProvider.dao.SalesAdvanceRefundMapper;
import com.kgc.skProvider.service.SalesAdvanceRefundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SalesAdvanceRefundServiceImpl implements SalesAdvanceRefundService {

    private Logger logger = LoggerFactory.getLogger(SalesAdvanceRefundServiceImpl.class);

    @Autowired
    private SalesAdvanceRefundMapper salesAdvanceRefundMapper;

    @Override
    public ResponseMsg selectSalesAdvanceRefundByPage(Map<String, Object> map) {
        logger.info("SalesAdvanceRefundServiceImpl selectSalesAdvanceRefundByPage params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesAdvanceRefund> pageInfo = new PageInfo<SalesAdvanceRefund>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesAdvanceRefund> list=salesAdvanceRefundMapper.selectSalesAdvanceRefundByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.info("SalesAdvanceRefundServiceImpl selectSalesAdvanceRefundByPage success {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertSalesAdvanceRefund(Map<String, Object> map) {
        logger.info("SalesAdvanceRefundServiceImpl insertSalesAdvanceRefund params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesAdvanceRefund salesAdvanceRefund =mapper.convertValue(map.get("salesAdvanceRefund"),SalesAdvanceRefund.class);
        List<?> rawDetails = (List<?>) map.get("details");
        List<SalesAdvanceRefundDetail> details=mapper.convertValue(rawDetails,new TypeReference<List<SalesAdvanceRefundDetail>>(){});
        int result=salesAdvanceRefundMapper.insertSalesAdvanceRefund(salesAdvanceRefund);
        try {
            for(SalesAdvanceRefundDetail detail:details){
                salesAdvanceRefundMapper.insertSalesAdvanceRefundDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("SalesAdvanceRefundServiceImpl insertSalesAdvanceRefund success {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesAdvanceRefundByCode(Map<String, Object> map) {
        logger.info(" selectSalesAdvanceRefundByCode params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        SalesAdvanceRefund salesAdvanceRefund =salesAdvanceRefundMapper.selectSalesAdvanceRefundByCode(map);
        List<SalesAdvanceRefundDetail> list=salesAdvanceRefundMapper.selectSalesAdvanceRefundDetail(map);
        salesAdvanceRefund.setDetails(list);
        responseMsg.setData(salesAdvanceRefund);
        logger.debug("selectSalesAdvanceRefundByCode success {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg updateSalesAdvanceRefund(Map<String, Object> map) {
        logger.info("SalesAdvanceRefundServiceImpl updateSalesAdvanceRefund params {}",map.toString());
        deleteSalesAdvanceRefund(map);
        ResponseMsg responseMsg =insertSalesAdvanceRefund(map);
        logger.debug("updateSalesAdvanceRefund success {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesAdvanceRefund(Map<String, Object> map) {
        logger.info("SalesAdvanceRefundServiceImpl deleteSalesAdvanceRefund params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1=salesAdvanceRefundMapper.deleteSalesAdvanceRefund(map.get("docCode").toString());
        int result2=salesAdvanceRefundMapper.deleteSalesAdvanceRefundDetail(map.get("docCode").toString());
        if(result1>0 && result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("deleteSalesAdvanceRefund success {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg checkSalesAdvanceRefund(Map<String, Object> map) {
        logger.info("SalesAdvanceRefundServiceImpl checkSalesAdvanceRefund params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1=salesAdvanceRefundMapper.checkSalesAdvanceRefund(map);
        if(result1>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("checkSalesAdvanceRefund success {}",responseMsg.toString());
        return responseMsg;
    }
}
